package cn.itcast.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneIndexDeleteTest {



    @Test
    public void testDeleteIndex() throws IOException {

        //不直接写死，而让其自动匹配最有解
        Directory directory = FSDirectory.open(new File("indexDir"));

        //分词器，想用中文，可惜过时了
        Analyzer analyzer = new IKAnalyzer();

        //索引写入时的配置对象
        IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST,analyzer);

        //每次使用时候重新创建（相当于把之前所有的内容清空）
        //conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

        //索引写入对象
        IndexWriter indexWriter = new IndexWriter(directory,conf);

        //删除所有
       //indexWriter.deleteAll();

        Query query = NumericRangeQuery.newLongRange("id",3L,3L,true,true);

        indexWriter.deleteDocuments(query);

        indexWriter.commit();

        indexWriter.close();

    }





}
